﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using System.Xml;
using System.Threading;
using MyLib.Utils;

namespace MyLib.Configuration
{
    public static class ConfigurationManager
    {
        public enum ConfigFileType
        {
            WebConfig,
            FileConfig,
            EmbeddedConfig,
            HttpConfig
        }

        static NameValueCollection _appSettings = new NameValueCollection();
        static ConnectionStringSettingsCollection _conectionStrings = new ConnectionStringSettingsCollection();
        static System.Configuration.Configuration _conf = null;
        static ConfigFileType _configFileType;

        static ConfigurationManager()
        {
            Config(ConfigFileType.WebConfig, "");
        }
        
        public static void Config(ConfigFileType configFileType, string fileName)
        {
            _configFileType = configFileType;
            if (configFileType == ConfigFileType.WebConfig)
            {
                _appSettings = System.Configuration.ConfigurationManager.AppSettings;
                _conectionStrings = System.Configuration.ConfigurationManager.ConnectionStrings;
            }
            else
            {
                ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                map.ExeConfigFilename = fileName;
                _conf = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                if (_conf != null)
                {
                    foreach (KeyValueConfigurationElement keyvalue in _conf.AppSettings.Settings)
                    {
                        _appSettings.Add(keyvalue.Key, keyvalue.Value);
                    }

                    _conectionStrings = _conf.ConnectionStrings.ConnectionStrings;
                }
            }
        }

        public static NameValueCollection AppSettings
        {
            get
            {
                return _appSettings;
            }
            set
            {
                _appSettings = value;
            }
        }

        public static ConnectionStringSettingsCollection ConnectionStrings
        {
            get
            {
                return _conectionStrings;
            }
            set
            {
                _conectionStrings = value;
            }
        }

        public static object GetSection(string sectionName)
        {
            if (_configFileType == ConfigFileType.FileConfig)
            {
                if (_conf != null)
                {
                    return _conf.GetSection(sectionName);
                }
            }

            return System.Configuration.ConfigurationManager.GetSection(sectionName);
        }

        public static ConnectionStringSettings GetConnectionString(string dbName)
        {
            if (string.IsNullOrEmpty(dbName))
            {
                if (ConfigurationManager.ConnectionStrings.Count > 1)
                {
                    dbName = ConfigurationManager.ConnectionStrings[1].Name;
                }
                else if (ConfigurationManager.ConnectionStrings.Count > 0)
                {
                    dbName = ConfigurationManager.ConnectionStrings[0].Name;
                }
            }

            if (ConfigurationManager.ConnectionStrings[dbName] == null)
            {
                throw new ArgumentNullException("ConnectionStrings:" + dbName);
            }
            else
            {
                return ConfigurationManager.ConnectionStrings[dbName];
            }
        }
    }
}
